package Server.Model.Base;

import common.routine.JsonUtils;
import db.Data_ninedt_user;
import net.sf.json.JSONObject;
import redis.protocal.DbInfoMgr;
import redis.protocal.UserRedisInfo;

//gold使用，系统当用户掉线的时候会清除SHMUSER,这个包装了一层，避免扣除金币的时候系统报错
public class SecShmUser {
	private SHMUser memUser = null;//此用户在线用这个
	private Data_ninedt_user redisInfo = null;//用户不在线用这个
	public SecShmUser(SHMUser user){
		memUser = user;
	}
	public SecShmUser(int uid){
		String redisData = DbInfoMgr.getInstance().getUserByUid(uid);
		if(redisData != null)
			redisInfo = JsonUtils.deserialize(redisData,Data_ninedt_user.class);
	}
	public boolean isAndroidUser(){
		if(memUser != null){
			return memUser.isAndroidPlayer();
		}else{
			return redisInfo.m_lUid >= 400000 && redisInfo.m_lUid <= 450000;
		}
	}
	public int getCoin(int add){
		if(memUser != null){
			return memUser.getCoin(add);
		}else{
			if(redisInfo == null)
				return 0;
			if(add == 0){
				return redisInfo.m_lTotalCoin;
			}else{
				redisInfo.m_lTotalCoin += add;
				if(redisInfo.m_lTotalCoin < 0){
					redisInfo.m_lTotalCoin = 0;
				}
				DbInfoMgr.getInstance().setUserDataByUid(redisInfo.m_lUid,JsonUtils.serialize(redisInfo));
				return redisInfo.m_lTotalCoin;
			}
		}
	}
	public double getRmb1v1(double add){
		if(memUser != null){
			if(add == 0)
				return memUser.getRmb1v1Value();
			memUser.addRmb1v1Value(add);
			return memUser.getRmb1v1Value();
		}else{
			if(redisInfo == null)
				return 0.0;
			String redisData = DbInfoMgr.getInstance().getUserRedisSaveInfo("" + redisInfo.m_lUid);
			if(redisData == null){
				return 0.0;
			}
			
			UserRedisInfo userRedis = new UserRedisInfo(JSONObject.fromObject(redisData));
			userRedis.addUserRmb1_1(add);
			DbInfoMgr.getInstance().setUserRedisSaveInfo("" + redisInfo.m_lUid, userRedis.getUserInfoStr());
			return userRedis.getUserRmb1_1();
		}
	}
	public int getUid(){
		if(memUser != null){
			return memUser.m_lUid;
		}else{
			if(redisInfo == null)
				return 0;
			return redisInfo.m_lUid;
		}
	}
}
